#include <iostream>
#include <memory>
#include <string>

using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;
using std::string;

struct FILEClose
{
    void operator()(FILE* ptr) const
    {
        if(ptr)
        {
            fclose(ptr);
            cout << "fclose(ptr)" << endl;
        }
    }
};

void test()
{
    //从智能指针得到裸指针可以使用成员函数get
    string msg("helloworld\n");
    unique_ptr<FILE, FILEClose> up(fopen("wd.txt", "a+"));
    fwrite(msg.c_str(), 1, msg.size(), up.get());
    /* fclose(up.get()); */
    /* while(1){} */
}

void test2()
{
    //从智能指针得到裸指针可以使用成员函数get
    string msg("helloworld\n");
    FILEClose fc;
    shared_ptr<FILE> sp(fopen("wd2.txt", "a+"), fc);
    fwrite(msg.c_str(), 1, msg.size(), sp.get());
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

